var prefix = jsctx + "/acctTenantOperator";

var acctContract = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageAcctContract",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            operatorStationId: $('#operatorStationId').val(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'createdTime', title: '上传时间',
                            width: '200'
                        },
                        {
                            field: 'contractName', title: '合同名称',
                            width: '500'
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '200',
                            formatter: function (value, row, index) {

                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="预览"  onclick="acctContract.invoiceModal(\''
                                    + row.contractPdf
                                    + '\')">预览</a> ';

                                var b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="下载" onclick="acctContract.fileDownload(\''
                                    + row.contractName + '\',\'' + row.contractPdf
                                    + '\')">下载</a> ';

                                var c = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="删除" onclick="acctContract.deleteAcctContract(\''
                                    + row.contractId
                                    + '\')">删除</a> ';

                                return a + b + c;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    var isajaxsend = true;
    function addAcctContract() {
        $('#contractImg').attr('src', "");
        $(".successTips").addClass("hide");
        $('#exampleModal').modal('show')
        $("#addAcctContract").unbind("click").bind("click", function () {
            var formData = new FormData($("#contentForm")[0]);
            console.log(formData)
            if (isajaxsend) {
                $.ajax({
                    url: prefix + "/saveAcctContract",
                    type: "post",
                    data: formData,
                    dataType: "json",
                    processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                    contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                    beforeSend: function () {
                        isajaxsend = false
                    },
                    success: function (data) {
                        if (data.status == 200) {
                            isajaxsend = true
                            parent.layer.msg("操作成功");
                            $('#exampleModal').on('hidden.bs.modal', function () {
                                document.getElementById("contentForm").reset();
                            })
                            $('#exampleModal').modal('hide')
                            reLoad();
                        } else {
                            isajaxsend = true
                            parent.layer.alert(data.message);
                            $('#exampleModal').on('hidden.bs.modal', function () {
                                document.getElementById("contentForm").reset();
                            })
                            //  $('#exampleModal').modal('hide')
                        }

                    }
                })

            }
        })

    }

    function fileDownload(contractName, contractPdf) {
        $.fileDownload(
            prefix + "/AcctContractDownload", { // 后台请求链接
                httpMethod: "GET",　　　　　　　　// 请求方式
                contentType: 'application/json',　 // 请求头
                data: {
                    contractPdf: contractPdf,
                    contractName: contractName
                },
                prepareCallback: function (url) {
                    layer.msg("文件下载中...", {time: -1})
                },
                abortCallback: function (url) {
                    // 异常终止
                    layer.msg("文件下载异常！！");
                },
                successCallback: function (url) {
                    layer.msg("文件下载成功！！", {time: 1500});
                },
                failCallback: function (data) {
                    var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                    layer.msg(JSON.parse(str).message);
                }
            })
    }

    function deleteAcctContract(contractId) {
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/deleteAcctContract",
                type: "post",
                data: {
                    'contractId': contractId
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("停用成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }


    function invoiceModal(contractPdf) {
        var contractPath = $("#contractPath").val();
        $("#myFrameId").attr("src", contractPath + contractPdf);
        setTimeout(function () {
            layer.open({
                type: 1,
                title: '合同预览',
                closeBtn: false,
                shade: 0.4,
                shift: 7,
                maxWidth: '622px',
                shadeClose: true,
                content: $("#invoiceModal"),
                btn: ['取消'],
                btn1: function (index) {//layer.msg('yes');    //取消
                    layer.close(index)
                },

            });
        }, 100)


    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        fileDownload: fileDownload,
        deleteAcctContract: deleteAcctContract,
        addAcctContract: addAcctContract,
        invoiceModal: invoiceModal
    };
})();
